package birdsong.dummy;

import birdsong.comm.ServerComm;
import birdsong.comm.ServerSideMessage;

//New imports
import java.net.*;
import java.util.Map;
import java.util.TreeMap;
import java.io.*;


public class ServerCommDummy implements ServerComm {
       
        private Map chatClients = new TreeMap();
        private PrintWriter out;
       
        @Override
        public void start() {
                System.out.println ("New Communication Thread Started");

            try {
               
                 PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
                 BufferedReader in = new BufferedReader(new InputStreamReader( clientSocket.getInputStream()));
                 String inputLine;
                 while ((inputLine = in.readLine()) != null)
                     {
                      System.out.println ("Server: " + inputLine);
                      out.println(inputLine);
                      if (inputLine.equals("exit"))
                          break;
                     }
                 out.close();
                 in.close();
                 clientSocket.close();
                }
            catch (IOException e)
                {
                 System.err.println("Problem with Communication Server");
                 System.exit(1);
                }
            }
        }

        @Override
        public boolean hasNextMessage() {
                // TODO Auto-generated method stub
                return false;
        }

        @Override
        public ServerSideMessage getNextMessage() {
                // TODO Auto-generated method stub
                return null;
        }

        @Override
        public void sendNewFollower(String toNickname, String newFollower) {
                // TODO Auto-generated method stub
               
        }

        @Override
        public void sendError(String toNickname, String error) {
                 for( Iterator i=this.chatClients.keySet().iterator(); i.hasNext(); )
                    {
                      String username = (String)i.next();
                      if( !username.equalsIgnoreCase( toNickname ) )
                      {
                        ServerCommDummy client = ( ServerCommDummy )this.chatClients.get( username );
                        client.ErrorMessage(toNickname, error);
                      }
                    }
               
        }

        @Override
        public boolean clientIsConnected(String nickname) {
                // TODO Auto-generated method stub
                return false;
        }

        @Override
        public void sendClientConnected(String toNickname, String userConnected) {
                for(Iterator i=this.chatClients.keySet().iterator(); i.hasNext();)
                   {
                      String username = (String)i.next();
                      if( !username.equalsIgnoreCase(toNickname) )
                     {
                        ServerCommDummy client = ( ServerCommDummy )this.chatClients.get(username);
                        client.UserConnected(userConnected);
                      }
                    }
        }

        @Override
                  public void UserConnected(String user)
                  {
                     out.println( "User" + user + "Connected");
                     out.flush();
                  }

        @Override
        public void sendClientDisconnected(String toNickname, String userDisconnected) {
        for(Iterator i=this.chatClients.keySet().iterator(); i.hasNext();)
           {
             String username = (String)i.next();
             if( !username.equalsIgnoreCase(toNickname) )
             {
               ServerCommDummy client = ( ServerCommDummy )this.chatClients.get( username );
               client.UserDisconnected(userDisconnected);
             }
           }
        }


        @Override
        public void UserDisconnected(String user)
         {
           out.println( "User" + user + "Disconnected");
           out.flush();
         }
       
        @Override
         public void privateMessage( String from, String message )
          {
            out.println( "PRIVATE" + from + ":" + message );
            out.flush();
          }
       
        @Override
         public void ErrorMessage( String from, String message )
          {
            out.println( "ERROR" + from + ":" + message );
            out.flush();
          }
       
        @Override
        public void sendBirdsongMessage(String fromNicename, String toNickname,String message) {
                for( Iterator i=this.chatClients.keySet().iterator(); i.hasNext(); )
            {
              String username = (String)i.next();
              if( username.equalsIgnoreCase(toNickname))
              {
                ServerCommDummy client = (ServerCommDummy)this.chatClients.get(username);
                client.privateMessage(fromNicename, message);
              }
            }
        }

        @Override
        public void sendNewUnfollow(String toNickname, String newFollower) {
                // TODO Auto-generated method stub
               
        }
}
